package com.atguigu.spzx.manager.service.system.impl;

import com.atguigu.spzx.manager.mapper.SysRoleMapper;
import com.atguigu.spzx.manager.mapper.SysUserRoleMapper;
import com.atguigu.spzx.manager.service.system.SysRoleService;
import com.atguigu.spzx.model.dto.system.SysRoleDto;
import com.atguigu.spzx.model.entity.system.SysRole;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Override
    public PageInfo<SysRole> findByPage(Integer pageNum, Integer pageSize, SysRoleDto sysRoleDto) {
        PageHelper.startPage(pageNum,pageSize);
        List<SysRole> sysRoleList = sysRoleMapper.selectByPage(sysRoleDto);
        PageInfo<SysRole> pageInfo = new PageInfo<>(sysRoleList);
        return pageInfo;
    }

    @Override
    public void saveRole(SysRoleDto sysRoleDto) {
        sysRoleMapper.saveRole(sysRoleDto);
    }

    @Override
    public void updateRole(SysRoleDto sysRoleDto) {
        sysRoleMapper.updateRole(sysRoleDto);
    }

    @Override
    public SysRole selectRoleById(Long id) {
        return sysRoleMapper.selectRoleById(id);
    }

    @Override
    public void deleteSysRoleById(Long roleId) {
        sysRoleMapper.deleteSysRoleById(roleId);
    }

    @Override
    public Map<String, Object> getAllRolesAndRoleIdByUserId(Long userId) {
        // 所有角色集合
        List<SysRole> sysRoles = sysRoleMapper.selectAllRole();
        // 当前用户拥有的角色id
        List<Long> roleIds = sysUserRoleMapper.selectRoleIdByUserId(userId);
        Map<String,Object> map = new HashMap<>();
        map.put("allRoles",sysRoles);
        map.put("userRoleIds",roleIds);
        return map;
    }
}
